package com.ls.pattern.strategy.pay.payport;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 支付策略
 *
 * @author 挥之以墨
 */
public enum PayStrategy {

    /**
     * 支付策略可以及实例
     */
    ALI_PAY("aliPay", new AliPay(), "支付宝"),
    WECHAT_PAY("wechatPay", new WechatPay(), "微信支付"),
    UNION_PAY("unionPay", new UnionPay(), "银联支付");

    private String paymentKey;
    private BasePayment payment;
    private String payTypeName;

    private static Map<String, BasePayment> PAYS = new HashMap<>();
    private static Map<String, String> PAY_TYPE_MAP = new HashMap<>();
    private static BasePayment defaultPay = ALI_PAY.payment;

    static {
        for (PayStrategy value : PayStrategy.values()) {
            PAYS.put(value.paymentKey, value.payment);
            PAY_TYPE_MAP.put(value.paymentKey, value.payTypeName);
        }
    }

    PayStrategy(String paymentKey, BasePayment payment, String payTypeName) {
        this.paymentKey = paymentKey;
        this.payment = payment;
        this.payTypeName = payTypeName;
    }

    /**
     * 获取支付抽屉
     *
     * @return 支付抽屉集合
     */
    public static Map<String, String> getPayTypes() {
        return PAY_TYPE_MAP;
    }

    public static BasePayment getPayStrategy(String paymentKey) {
        return Optional.ofNullable(PAYS.get(paymentKey)).orElse(defaultPay);
    }

}
